<template>
  <div class="main" v-loading="loading">
      <div class="box">
          <!-- 菜品 -->
          <el-card class="box-card" v-for="(item, index) in menuList" :key="index">
              <div class="dishes-content">
                  <img :src="item.mphoto" alt="">
                  <div class="title">
                      <div>{{item.mname}}</div>
                      <div class="remark">{{item.mremark}}</div>
                  </div>
                  <div class="money">
                      <div>￥{{item.mprice}}</div>
                      <el-input-number v-model="item.num" @change="handleChange" :min="0" size="mini"></el-input-number>
                  </div>
              </div>
          </el-card>
      </div>
      <!-- 购物车 -->
      <div class="car">
          <el-popover
          placement="right"
          @show="showCar"
          trigger="hover">
              <el-table :data="gridData">
                  <el-table-column width="150" property="mname"></el-table-column>
                  <el-table-column width="100" property="num"></el-table-column>
              </el-table>
              <div class="tatal">
                  <div>数量：{{carNumber}}</div>
                  <div>总计：{{carMoney}}￥</div>
                  <div style="height: 30px;">
                    <el-button round size="mini" @click="showDialog">下单</el-button>
                  </div>
              </div>
              <el-button round icon="el-icon-shopping-cart-full" slot="reference">购物车</el-button>
          </el-popover>
      </div>
      <!-- 添加订单弹窗 -->
      <el-dialog
      title="添加订单"
      :visible.sync="dialogVisible"
      custom-class="order-dialog"
      width="500px">
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label-width="10px">
                  <el-radio-group v-model="ruleForm.orderType" >
                      <el-radio-button :label="0">堂食</el-radio-button>
                      <el-radio-button :label="1">外卖</el-radio-button>
                  </el-radio-group>
              </el-form-item>
              <el-form-item label="称呼：" prop="orderContact">
                  <el-input el-input v-model="ruleForm.orderContact" placeholder="请输入称呼"></el-input>
              </el-form-item>
              <el-form-item label="联系方式：" prop="orderContactWay">
                  <el-input el-input v-model="ruleForm.orderContactWay" placeholder="请输入联系方式"></el-input>
              </el-form-item>
              <!-- 根据不同的订单类型显示不同文字 -->
              <el-form-item :label="ruleForm.orderType == 0 ? '桌号：' : '地址：'" prop="orderPlace">
                  <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="ruleForm.orderPlace"></el-input>
              </el-form-item>
              <div class="dishes-content content-box" v-for="(item, index) in gridData" :key="index">
                  <img :src="item.mphoto" alt="">
                  <div class="title">
                      <div>{{item.mname}}</div>
                      <div class="remark">{{item.mremark}}</div>
                  </div>
                  <div class="money">
                      <div>￥{{item.mprice}}</div>
                      <el-input-number v-model="item.num" @change="handleChange" :min="0" size="mini"></el-input-number>
                  </div>
              </div>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="confirm">确 定</el-button>
          </span>
      </el-dialog>
  </div>
</template>

<script>
export default {
    data() {
      return {
        num: 1,
        dialogVisible: false,
        menuList: [],
        gridData: [],
        carNumber: 0,
        carMoney: 0,
        ruleForm: {
            orderId: "",
            userid: JSON.parse(window.sessionStorage.getItem('user')).userID,
            orderType: 0,
            orderContact: '',
            orderContactWay: '',
            orderPlace: '',
            orderStatus: 1,
            orderPrice: "",
            orderNum: '',
        },
        rules: {
            orderContact: [
                { required: true, message: '请输入称呼', trigger: 'blur' },
            ],
            orderContactWay: [
                { required: true, message: '请输入联系方式', trigger: 'blur' },
            ],
            orderPlace: [
                { required: true, message: '请输入内容', trigger: 'blur' },
            ],
        },
        loading: false,
        form: {
            query: {
                id: "",
                mname: "",
                mprice: "",
                mphoto: "",
                mremark: ""
            },
            pageInfo: {
                pageNum: 1,
                pageSize: 200
            }
        },
      };
    },
    mounted () {
        // 页面加载获取菜品列表
        this.getMenu()
    },
    methods: {
        handleChange(value) {
            console.log(value);
        },
        // 获取菜品列表
        getMenu () {
            this.loading = true
            this.$http.post('menu/queryAllMenuUnit', this.form).then(res => {
                this.loading = false
                if (res.data.meta.status == 200) {
                    console.log(res.data)
                    for (let i=0; i<res.data.data.list.length; i++) {
                        res.data.data.list[i].num = 0// 默认菜品数量为0
                    }
                    this.menuList = res.data.data.list
                } else {
                    this.$message({type: 'error', message: '获取失败'})
                }
            })
        },
        // 显示购物车
        showCar () {
            let carData = []
            let carNumber = 0
            let carMoney = 0
            // 获取数量不为0的菜品
            for (let i=0; i<this.menuList.length; i++) {
                if (this.menuList[i].num > 0) {
                    carData.push(this.menuList[i])
                    carNumber += this.menuList[i].num
                    carMoney += (this.menuList[i].mprice * this.menuList[i].num)
                }
            }
            this.gridData = carData
            this.carNumber = carNumber
            this.carMoney = carMoney
        },
        // 添加订单
        confirm () {
            this.$refs['ruleForm'].validate((valid) => {// 预验证
                if (valid) {
                    if (this.gridData.length == 0) {// 没菜品不能添加
                        this.$message({type: 'error', message: '请添加菜品！'})
                        return
                    }
                    let menuIdList = []
                    let menuIdSumList = []
                    // 过滤数据
                    for (let i=0; i<this.gridData.length; i++) {
                        menuIdList.push(this.gridData[i].id)
                        menuIdSumList.push(this.gridData[i].num)
                    }
                    this.ruleForm.menuId = menuIdList.toString(',')
                    this.ruleForm.menuIdSum = menuIdSumList.toString(',')
                    this.ruleForm.orderNum = this.carNumber
                    this.ruleForm.orderPrice = this.carMoney
                    this.insertOrder()// 添加订单
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 展示菜单并重置参数
        showDialog () {
            this.ruleForm = {
                orderId: "",
                userid: JSON.parse(window.sessionStorage.getItem('user')).userID,
                orderType: 0,
                orderContact: '',
                orderContactWay: '',
                orderPlace: '',
                orderStatus: 1,
                orderPrice: "",
                orderNum: '',
            }
            this.dialogVisible = true
        },
        insertOrder () {
            this.$http.post('order/insertOrder', this.ruleForm).then(res => {
                if (res.data.meta.status == 200) {
                    this.$message({type: 'success', message: '下单成功，请耐心等待！'})
                    this.dialogVisible = false
                } else {
                    this.$message({type: 'error', message: '下单失败'})
                }
            })
        }
    }
}
</script>

<style scoped>
.box {
    display: flex;
    flex-wrap: wrap;
    width: 90%;
}
.el-card {
    width: 400px;
    margin: 10px ;
}
.dishes-content {
    display: flex;
    justify-content: space-between;
}
.dishes-content img {
    width: 90px;
    height: 90px;
}
.dishes-content .title {
    width: 45%;
    font-size: 18px;
    font-weight: 700;
}
.dishes-content .title .remark {
    margin-top: 20px;
    font-size: 14px;
    color: #999;
}
.dishes-content .money {
    font-size: 24px;
    text-align: right;
    color: red;
}
.dishes-content .money .el-input-number--mini {
    margin-top: 20px;
    width: 90px;
}
.car {
    position: fixed;
    right: 40px;
    bottom: 40px;
}
.tatal {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 50px;
    margin-top: 50px;
    border-radius: 100px;
    background: #2b4b6b;
    color: #FFF;
    line-height: 50px;
}
.content-box {
  width: 90%;
  margin: 0 auto;
  border: 1px solid #F2F2F2;
  padding: 20PX;
  border-radius: 5px;
}
</style>
<style>
.order-dialog .el-dialog__body{
  max-height: 500px;
    overflow: auto;
}
</style>